﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RZD_maps.Infrastructure;
using Microsoft.Practices.Prism.Events;
using RZD_maps.Modules.Transport.Views;
using RZD_maps.Modules.Transport.Models;
namespace RZD_maps.Modules.Transport.ViewModels
{
    public class ViewModel
    {
        public View View { get; set; }
        private Model model;

        private IEventAggregator eventAggregator;
        private SubscriptionToken subscriptionToken;

        public ViewModel()
        {
            model = new Model();
        }

        public ViewModel(IEventAggregator eventAggregator)
            : this()
        {
            this.eventAggregator = eventAggregator;
            SubscriptCommands();
        }

        public void SubscriptCommands()
        {
            TrainPositionEvent evntTrain = eventAggregator.GetEvent<TrainPositionEvent>();
            if (subscriptionToken != null)
            {
                evntTrain.Unsubscribe(subscriptionToken);
            }
            subscriptionToken = evntTrain.Subscribe(TrainEventHandler, ThreadOption.UIThread, false, TrainEventFilter);
        }
        public void TrainEventHandler(TrainPosition data)
        {
            model.ChangeData(data);
        }

        public bool TrainEventFilter(TrainPosition data)
        {
            return true;
        }

        public void GetLatLongById(int Id, out double Lat, out double Lng)
        {
            Lat = 0.0;
            Lng = 0.0;
            foreach (var item in model.list_data)
            {
                if (item.Id != Id) continue;
                Lat = item.Lat;
                Lng = item.Lng;
                break;
            }
        }

        public void TransferMapPosition(double lng, double lat, int zoom)
        {
            MapPositionItem item = new MapPositionItem();
            item.lng = lng;
            item.lat = lat;
            item.zoom = zoom;
            eventAggregator.GetEvent<MapPositionItemEvent>().Publish(item);
        }

        public void TransferGraphicInfo(string data)
        {
            InfoForGraphics item = new InfoForGraphics();
            item.name = data;
            eventAggregator.GetEvent<InfoForGraphicsEvent>().Publish(item);
        }
    }
}
